package cn.itrip.dao.service;

import cn.itrip.beans.pojo.*;

import cn.itrip.beans.vo.comment.ItripListCommentVO;
import cn.itrip.beans.vo.comment.ItripScoreCommentVO;
import cn.itrip.dao.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Tequila
 **/
@Service
public class ItripCommentServiceImpl implements ItripCommentService {
    @Autowired
    private ItripCommentMapper itripCommentMapper;
    @Autowired
    private ItripUserMapper itripUserMapper;
    @Autowired
    private ItripHotelMapper itripHotelMapper;
    @Autowired
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Autowired
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Autowired
    private ItripLabelDicMapper itripLabelDicMapper;
    @Override
    public cn.itrip.common.Page<ItripListCommentVO> getCommentList(Map<String, Object> paramMap) {
        cn.itrip.common.Page<ItripListCommentVO> resultPage = new cn.itrip.common.Page();
        Integer pageNo = (Integer) paramMap.get("pageNo");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        Page<ItripComment> page = new Page<>(pageNo,pageSize);
        QueryWrapper<ItripComment> q = new QueryWrapper<>();
        q.eq("hotelId",paramMap.get("hotelId"));
        if (paramMap.get("isHavingImg")!= null){

            q.eq("isHavingImg",paramMap.get("isHavingImg"));
        }
        if (paramMap.get("isOk")!=null){

            q.eq("isOk",paramMap.get("isOk"));
        }
        Page<ItripComment> itripCommentPage = itripCommentMapper.selectPage(page, q);
        List<ItripComment> records = itripCommentPage.getRecords();
        // vo列表
        List<ItripListCommentVO> voList = new ArrayList<>();
        for (ItripComment itripComment : records) {
            ItripListCommentVO itripListCommentVO = new ItripListCommentVO();
            itripListCommentVO.setContent(itripComment.getContent());
            itripListCommentVO.setId(itripComment.getId());
            itripListCommentVO.setScore(itripComment.getScore());

            ItripUser itripUser = itripUserMapper.selectById(itripComment.getUserid()); // 根据用户id查询用户名
            if (itripUser!= null){

                itripListCommentVO.setUserCode(itripUser.getUsercode());
            }
            ItripHotel itripHotel = itripHotelMapper.selectById(itripComment.getHotelid());// 根据酒店id 查询酒店星级
            if (itripHotel!=null){

                itripListCommentVO.setHotelLevel(itripHotel.getHotellevel());
            }
            // 根据orderId 查询入住时间
            ItripHotelOrder itripHotelOrder = itripHotelOrderMapper.selectById(itripComment.getOrderid());
            if (itripHotelOrder!=null){

                itripListCommentVO.setCheckInDate(itripHotelOrder.getCheckindate());
                // 根据order中的roomId 查询room对象
                ItripHotelRoom itripHotelRoom = itripHotelRoomMapper.selectById(itripHotelOrder.getRoomid());
                if (itripHotelRoom!=null){

                    itripListCommentVO.setRoomTitle(itripHotelRoom.getRoomtitle());
                }
            }
            // 查询出游类型
            ItripLabelDic itripLabelDic = itripLabelDicMapper.selectById(itripComment.getTripmode());
            if (itripLabelDic!=null){

                itripListCommentVO.setTripModeName(itripLabelDic.getName());
            }
            // 评论创建时间
            itripListCommentVO.setCreationDate(itripComment.getCreationdate());
            itripListCommentVO.setIsHavingImg(itripComment.getIshavingimg());
            voList.add(itripListCommentVO);
        }
        resultPage.setCurPage((int) itripCommentPage.getCurrent());
        resultPage.setPageCount((int) itripCommentPage.getPages());
        resultPage.setRows(voList);
        resultPage.setTotal((int) itripCommentPage.getTotal());
        resultPage.setPageSize((int) itripCommentPage.getSize());
        return resultPage;

    }

    @Override
    public ItripScoreCommentVO getHotelScore(String hotelId) {

        return itripCommentMapper.getHotelScore(hotelId);
    }

    @Override
    public Map<String, Object> getCount(Integer hotelId) {
        return itripCommentMapper.getCount(hotelId);
    }


}
